package samples
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.BlendMode;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BitmapFilter;
	import flash.filters.BlurFilter;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.media.Camera;
	import flash.media.Video;
	
	import marcel.debug.*;
	import marcel.utils.BitmapUtils;
	
	public class WebcamSample extends Sprite
	{
		protected var _ratio:Number = 1;
		protected var _size:Point = new Point(320 * _ratio, 240 * _ratio);
		protected var _video:Video = new Video();
		protected var _background:BitmapData = new BitmapData(_size.x, _size.y, false);
		protected var _bg:Bitmap = new Bitmap(_background, PixelSnapping.AUTO, true);
		
		
		protected var _prev:BitmapData = new BitmapData(_size.x, _size.y, false);
		protected var _current:BitmapData = new BitmapData(_size.x, _size.y, false);
		protected var _result:BitmapData = new BitmapData(_size.x, _size.y, false);
		protected var _done:BitmapData = new BitmapData(_size.x, _size.y, false);
		protected var _render:Bitmap = new Bitmap(_done, PixelSnapping.AUTO, true);
		
		protected var _ready:Boolean = false;
		
		
		public function WebcamSample()
		{
			super();
			_initView();
			_initListener();
		}
		
		protected function _initView():void {
			_initWebcam();
			//_render.x = _size.x;
			addChild(_render);
			_bg.x = _size.x * 2;
			addChild(_bg);
			
		}
		
		protected function _initWebcam():void {
			var _cam:Camera = Camera.getCamera();
			_video.attachCamera(_cam);
			addChild(_video);
		}
		
		protected function _initListener():void {
			addEventListener(Event.ENTER_FRAME, _onEnterFrame);	
		}
		
		protected function _removeListener():void {
			removeEventListener(Event.ENTER_FRAME, _onEnterFrame);	
		}
		
		protected function _onEnterFrame(e:Event):void {
			_current.draw(_video);
			if (!_ready) {
				_done.draw(_current);
				_done.draw(_prev, new Matrix(), null, BlendMode.DIFFERENCE);
				_done.threshold(_done,_done.rect,_done.rect.topLeft,">",0xFF111111,0xFF00FF00,0x00FFFFFF,false); 
				var _h:Vector.<Vector.<Number>> = _done.histogram();
				_ready = _h[1][0] > 0 && _h[1][0] < (_size.x * _size.y / 5);
				if (_ready) {
					_background.draw(_current);
					_background.applyFilter(_background, _background.rect, new Point(), _getColorMatrixFilters());
				}
				
			} else {
				_done.draw(_current);
				_done.applyFilter(_done, _done.rect, new Point(), _getColorMatrixFilters());
				_done.applyFilter(_done, _done.rect, new Point(), new BlurFilter());
				_done.draw(_background, new Matrix(), null, BlendMode.DIFFERENCE, _done.rect, true);
				_done.threshold(_done, _done.rect, new Point(), ">", 0xFF101010, 0xFFFFFFFF);
				_done.threshold(_done, _done.rect, new Point(), "<=", 0xFF101010, 0x00000000);				
				/*
				var _aPalette:Array = new Array();
				for (var i:int = 0; i < 0xFF ; i++) _aPalette.push(i < 0xFFF ? 0 : 0xFFFFFFFF);
				_done.paletteMap(_done, _done.rect, new Point(), null, null, _aPalette);
				_done.copyChannel(_done, _done.rect, new Point(), BitmapDataChannel.BLUE, BitmapDataChannel.ALPHA);
				*/
				cacheAsBitmap = true;
				_render.blendMode = BlendMode.ALPHA;
			}
			
			_prev = _current.clone();
		}
		
		protected function _getColorMatrixFilters():BitmapFilter {
			return new ColorMatrixFilter([0.212671,0.715160,0.072169,0,0,0.212671,0.715160,0.072169,0,0,0.212671,0.715160,0.072169,0,0,0,0,0,1,0]);
		}

	}
}